//
//  NSDate+NSDateAddition.m
//  NJYouthOlypic
//
//  Created by Xiu on 6/24/13.
//  Copyright (c) 2013 Xiu. All rights reserved.
//

#import "NSDate+NSDateAddition.h"

@implementation NSDate (NSDateAddition)

+ (NSDate*)dateFromString:(NSString*)dateStr
                formatted:(NSString*)format{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format];
    NSDate* formattedDate = [formatter dateFromString:dateStr];

    return formattedDate;
}


- (BOOL)isSameDayTo:(NSDate*)date{
    NSDate* fromDate = [self formatedDateFrom:self];
    NSDate* toDate = [self formatedDateFrom:date];
    
    if ([fromDate isEqualToDate:toDate]) {
        return YES;
    }
    return NO;
}

- (NSDate*)thisDay{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString* dateStr = [formatter stringFromDate:self];
    NSDate* formattedDate = [formatter dateFromString:dateStr];

    return formattedDate;
}
- (NSDate*)prevDay{
    NSCalendar *gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: self];
    components.day -= 1;
    NSDate* nextDay = [gregorian dateFromComponents:components];
    return nextDay;
}

- (NSDate*)nextDay{
    NSCalendar *gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: self];
    components.day += 1;
    NSDate* nextDay = [gregorian dateFromComponents:components];
    return nextDay;
}

- (NSDate*)formatedDateFrom:(NSDate*)date{
    NSCalendar *gregorian = [[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: date];
    return [gregorian dateFromComponents:components];
}

- (NSString*)formattedTimeString:(BOOL)is24Hour{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm"];
    //[formatter setDateStyle:NSDateFormatterNoStyle];
    //[formatter setTimeStyle:NSDateFormatterShortStyle];
    NSString* time = [formatter stringFromDate:self];
    return time;
}

- (NSString*)stringFormattedAsddMMyyyyHHmm{
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd.MM.yyyy HH:mm"];
    NSString* dateStr = [formatter stringFromDate:self];
    return dateStr;
}

- (BOOL)isLarerThan:(NSDate*)date{
    NSTimeInterval timeA = [self timeIntervalSince1970];
    NSTimeInterval timeB = [date timeIntervalSince1970];
    return timeA > timeB;
}

- (BOOL)isLessThan:(NSDate*)date{
    NSTimeInterval timeA = [self timeIntervalSince1970];
    NSTimeInterval timeB = [date timeIntervalSince1970];
    return timeA < timeB;
}

- (NSString*)dayOfWeek{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    // Find Sunday for the given date
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit
                                               fromDate:self];
    NSInteger dayOfWeek = [components weekday];
    
    switch (dayOfWeek) {
        case 1:
            return @"Sun";
            break;
        case 2:
            return @"Mon";
            break;
        case 3:
            return @"Tue";
            break;
        case 4:
            return @"Wed";
            break;
        case 5:
            return @"Thu";
            break;
        case 6:
            return @"Fri";
            break;
        case 7:
            return @"Sat";
            break;
        default:
            break;
    }
    return nil;
}

- (NSInteger)day{
    NSInteger day = 0;
    // Calendrical Calculate the Day and the day texture
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: self];
    day = [components day];
    return day;
}

- (NSInteger)month{
    // Calendrical Calculate the Day and the day texture
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: self];
    return [components month];
}

- (NSInteger)year{
    // Calendrical Calculate the Day and the day texture
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                 fromDate: self];
    return [components year];
}

- (NSInteger)hour{
    // Calendrical Calculate the Day and the day texture
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components:(kCFCalendarUnitHour)
                 fromDate: self];

    return [components hour];
}

- (NSInteger)min{
    // Calendrical Calculate the Day and the day texture
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components =
    [gregorian components:(kCFCalendarUnitMinute)
                 fromDate: self];

    return [components minute];
}

@end
